﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace danci
{
    public class FRMModel:INotifyPropertyChanged
    {
        private readonly string fndefault = Path.Combine(AppConfig.DataRoot,"words.txt");

        public List<string> wordlist = new List<string>();

        private int _index = 0;
        public int WordIndex
        {
            get
            {
                return _index;
            }
            set
            {
                this._index = value;
                if (this._index < 0) this._index = this.wordlist.Count - 1;
                if (this._index >= this.wordlist.Count) this._index = 0;
                raisePropertyChanged();
                
                //this.labNumber.Text = (this._index+1).ToString();
                //labWord.Text = wordlist[_index];
            }
        }

        private void raisePropertyChanged()
        {
            OnPropertyChanged(nameof(CurrentWord));
            OnPropertyChanged(nameof(CurrentWordDisp));
            OnPropertyChanged(nameof(WordIndex));
            OnPropertyChanged(nameof(TotalWords));
        }

        public int TotalWords
        {
            get
            {
                return this.wordlist.Count;
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        public string CurrentWord
        {
            get
            {
                if (wordlist.Count > _index)
                    return this.wordlist[_index];
                else
                    return "";
            }
        }

        internal void saveCurrent(string[] list)
        {
            this.wordlist.Clear();
            this.wordlist.AddRange(list);
            File.WriteAllLines(CurrentFn, this.wordlist);
        }

        public string CurrentWordDisp
        {
            get
            {
                if (wordlist.Count > _index)
                    return $"{_index+1}.{this.wordlist[_index]}";
                else
                    return "";
            }
        }

        internal void goPrev()
        {
            this.WordIndex++;
        }
        internal void goNext()
        {
            this.WordIndex--;
        }


        private string CurrentFn 
        {
            get
            {
                return Properties.Settings.Default.CurrentList;
            }
        }
        public string CurrentFnUI
        {
            get
            {
                return Path.GetFileName(this.CurrentFn);
            }
            set
            {
                OnPropertyChanged(nameof(CurrentFnUI));
            }
        }

        internal void loadList()
        {
            var fn = this.CurrentFn;
            if (!File.Exists(fn))
            {
                fn = this.fndefault;
            }
            setWordList(File.ReadAllLines(fn));
            WordIndex = 0;
        }

        internal void setWordList(string[] lines)
        {
            this.wordlist.Clear();
            this.wordlist.AddRange(lines);
            this.raisePropertyChanged();
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        
    }
}
